<?php
  require_once "function.php";
  $targetdate=getdate(time()-180*24*3600);
  $currentdate=getdate();

  $url1="http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/PatientMedicalRecordListQuery.aspx?SESSION=".$sessionid;

  $hidden=viewstate($url1);
  $r=new HttpRequest($url1);
  $r->setMethod(HTTP_METH_POST);
  $data=array('scrollLeft'=>'','scrollTop'=>'','__LASTFOCUS'=>'','NTUHWeb1$ChartInputTextBox'=>$_GET['id'],'__EVENTTARGET'=>'','__EVENTARGUMENT'=>'','__VIEWSTATE'=>$hidden['viewstate'],'__EVENTVALIDATION'=>$hidden['eventvalidation'],'NTUHWeb1$ButtonQuery'=>'查詢');
  $r->addPostFields($data);
  $r->send();
  $a=$r->getResponseBody();

  $dom=new DOMDocument;
  $dom->perserveWhiteSpace=FALSE;
  @$dom->loadHTML($a);
  foreach($dom->getElementsByTagName('input') as $element){
      if($element->getAttribute('name')=='NTUHWeb1$PatAccountListRecord1$personidHidden') $ID_no=$element->getAttribute('value');
  }
  $history="http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/ReportResultQuery.aspx?SESSION=".$sessionid."&PatClass=I&PersonID=".$ID_no."&Hosp=T0&DefaultQuery=EMRContent";

  $hidden2=viewstate($history);
  $r2=new HttpRequest($history);
  $r2->setMethod(HTTP_METH_POST);
  $data2=array('__EVENTTARGET'=>'ShowReport','__EVENTARGUMENT'=>'','__VIEWSTATE'=>$hidden2['viewstate'],'__EVENTVALIDATION'=>$hidden2['eventvalidation'],'DateTextBoxYearMonthDayStart$YearInput'=>$targetdate['year'],'DateTextBoxYearMonthDayStart$MonthInput'=>$targetdate['mon'],'DateTextBoxYearMonthDayStart$DayInput'=>$targetdate['mday'],'DateTextBoxYearMonthDayEnd$YearInput'=>$currentdate['year'],'DateTextBoxYearMonthDayEnd$MonthInput'=>$currentdate['mon'],'DateTextBoxYearMonthDayEnd$DayInput'=>$currentdate['mday']);
  $r2->addPostFields($data2);
  $r2->send();
  $a2=$r2->getResponseBody();

  $labDom=new DOMDocument();
  $labDom->preserveWhiteSpace=FALSE;
  $labDom->encoding='utf-8';
  @$labDom->loadHTML($a2);

  $labstrarray=array("HB","PLT","WBC","PT","PT INR","PTT","UN","CRE","AMY","AST","ALT","ALP","T-BIL","D-BIL","GGT","r-GT","Alb","Lipase","Alpha-Fetoprotein");
  $labitem=array();
  foreach($labDom->getElementsByTagName('td') as $element){
    if(@in_array($element->nodeValue,$labstrarray)){
      if($element->nodeValue=='GGT'){
          $labitem["r-GT"].=utf8_decode($element->nextSibling->nextSibling->nodeValue);
          continue;
      }
      $labitem["$element->nodeValue"].=utf8_decode($element->nextSibling->nextSibling->nodeValue);
    }
  }
  if(empty($labitem)) exit;

  foreach($labitem as $idx => $element){
    $test=array();
    $ar=array();
    $bt=array();
    $test=explode(') ',$element);
    foreach($test as $tt){
      if($tt==NULL) continue;
      $ar=explode(' (',$tt);
      $cu=explode(' ',$ar[0]);
      if($ar[1]-600>date('md')){
          $oldyear=$currentdate['year']-1;
          $ar[1]=$oldyear.$ar[1];
      }else $ar[1]=$currentdate['year'].$ar[1];
      $bt["$ar[1]"]=$cu;
    }
    krsort($bt);
    $labitem[$idx]=$bt;
  }
  echo '<table border="1" cellspacing="0" cellpadding="5"><tr><td>半年內檢驗項目</td><td colspan=2>最新</td><td></td><td colspan=2>最舊</td></tr>';
  foreach($labitem as $key => $element){
    reset($element);
    $mix=current($element);
    echo "<tr><td>".$key." (".$mix[1].")</td><td>".$mix[0]."</td><td>".key($element)."</td>";
    end($element);
    $mix2=current($element);
    echo "<td></td><td>".$mix2[0]."</td><td>".key($element)."</td></tr>";
  }
  echo "</table><p>本頁面參考 Portal 累積檢驗頁面，其檢體類別(blood, urine, ascites, pleural effusion, CSF, etc.)並未標明，故有可能摻雜非血液檢驗值，請小心使用。</p>";
?>
